<template>
  <el-row class="login_page">
    <!-- elementplus的栅格用法，24列 -->
    <el-col :span="12" :xs="0" class="title"></el-col>
    <el-col :span="12" :xs="24" class="form_container">
      <div class="form">
        <!-- 注册表单 -->
        <el-form v-if="isRegister" :model="registerDate" :rules="rules">
          <el-form-item>
            <h1>注册</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerDate.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
              v-model="registerDate.password" show-password></el-input>
          </el-form-item>
          <el-form-item prop="rePassword">
            <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码"
              v-model="registerDate.rePassword" show-password></el-input>
          </el-form-item>
          <el-form-item prop="userIdentity">
            <el-radio-group v-model="registerDate.userIdentity">
              <!-- <el-radio value="管理员">管理员</el-radio> -->
              <el-radio value="收容所">收容所</el-radio>
              <el-radio value="用户">用户</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="register">
              注册
            </el-button>
          </el-form-item>
          <el-form-item class="flex">
            <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
              ← 返回
            </el-link>
          </el-form-item>
        </el-form>
        <!-- 登录表单 -->
        <el-form v-else :model="registerDate" :rules="rules">
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerDate.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
              v-model="registerDate.password" show-password></el-input>
          </el-form-item>
          <el-form-item prop="userIdentity">
            <el-radio-group v-model="registerDate.userIdentity">
              <el-radio value="管理员">管理员</el-radio>
              <el-radio value="收容所">收容所</el-radio>
              <el-radio value="用户">用户</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="flex">
            <div class="flex">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
          </el-form-item>
          <el-form-item class="flex">
            <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
              注册 →
            </el-link>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { userRegisterService, userLoginService } from '@/api/user'
import { useTokenStore } from '@/stores/token.js'

const isRegister = ref(false)
const $router = useRouter()
const tokenStore = useTokenStore()

const registerDate = ref({
  username: '',
  password: '',
  rePassword: '',
  userIdentity: ''
})

const checkRePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次确认密码'))
  } else if (value !== registerDate.value.password) {
    callback(new Error('请确保两次输入的密码一样'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5-16位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5-16位非空字符', trigger: 'blur' }
  ],
  rePassword: [{ validator: checkRePassword, trigger: 'blur' }],
  userIdentity: [{ required: true, message: '请选择注册身份', trigger: 'change' }]
}

const register = async () => {
  if (!registerDate.value.userIdentity) {
    ElMessage.error('请选择注册身份')
    return
  }
  let result = await userRegisterService(registerDate.value)
  ElMessage.success('注册成功')
}

const login = async () => {
  if (!registerDate.value.userIdentity) {
    ElMessage.error('请选择登录身份')
    return
  }
  let result = await userLoginService(registerDate.value)
  ElMessage.success('登录成功')
  tokenStore.setToken(result.data)
  $router.push('/')
}

const clearRegisterData = () => {
  registerDate.value = {
    username: '',
    password: '',
    rePassword: '',
    userIdentity: ''
  }
}
</script>

<style scoped lang="scss">
.login_page {
  height: 100vh;
  width: 100%;
  background: url('@/assets/登录背景.jpg') no-repeat center / cover;

  .title {
    background: url('@/assets/流浪动物.png') no-repeat center center / 520px auto;
  }

  .form_container {
    position: relative;
  }

  .form {
    height: 35vh;
    width: 70%;
    background-color: rgb(224, 243, 248, 0.5);
    border-radius: 60px 60px 60px 60px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px 80px 120px 80px;

    h1 {
      font-size: 25px;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
